package com.zerdoor.service.impl;

import com.zerdoor.dao.ITaskListDao;
import com.zerdoor.service.ITaskListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/10/29.
 * 精选项目分类 业务层实现类
 */
@Service
public class TaskListServiceImpl implements ITaskListService {

    @Autowired
    private ITaskListDao taskListDao;

    @Override
    public List<Map<String, Object>> findTaskTypeByParent(int parent) throws Exception {



        return taskListDao.findTaskTypeByParent(parent);
    }

    @Override
    public List<Map<String, String>> findVipLevelset() throws Exception {

        return taskListDao.findVipLevelset();
    }

    @Override
    public List<Map<String, Object>> selectAllTaskInfo(Map<String, String> param) throws Exception {


        //判断佣金不为空
        if(param.get("budgetRegion") != null && !"".equals(param.get("budgetRegion")) ){

            String str[] = param.get("budgetRegion").split("-");

            param.put("budgetRegion_lower", str[0]);
            param.put("budgetRegion_upper", str[1]);


        }

        //判断截止时间不为空
        if(param.get("durplusDay") != null && !"".equals(param.get("durplusDay")) ){

            String str[] = param.get("durplusDay").split("-");

            param.put("durplusDay_lower", str[0]);
            param.put("durplusDay_upper", str[1]);


        }
        //判断客户信用不为空
        if(param.get("clientType") != null && !"".equals(param.get("clientType")) ){

            String str[] = param.get("clientType").split("-");

            param.put("start_trading", str[0]);
            param.put("end_trading", str[1]);


        }

        //查询任务信息数据
        List<Map<String, Object>> taskList = taskListDao.selectAllTaskInfo(param);




        return taskList;
    }

    @Override
    public List<Map<String, Object>> findTaskInfoNameByName(String name) throws Exception {


        return taskListDao.findTaskInfoNameByName(name);
    }


    /**
     * 获取剩余天数
     * @param publishTime 发布时间
     * @param bidTime 竞标时间
     * @return
     * @throws Exception
     */
    private int getDurplusDayNumber(String publishTime,int bidTime) throws Exception{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(publishTime);

        Calendar c = Calendar.getInstance();

        c.setTime(date);

        long time1 = c.getTimeInMillis();

        c.setTime(new Date());

        long time2 = c.getTimeInMillis();

        System.out.println(c.get(Calendar.DAY_OF_MONTH) + "");

        int dayNum = (int)(time2-time1)/(1000*60*60*24);

        return bidTime - dayNum;

    }


}
